/*
 *
 * @ProjectName:    my_umi_app
 * @Package:
 * @ClassName:      blog_article_api
 * @Description:    TypeScript文件
 * @Author:         SuPeiSen
 * @CreateDate:     2021/2/3 4:42 下午
 * @UpdateDate:     2021/2/3 4:42 下午
 * @UpdateRemark:   更新说明：
 * @Version:        1.0
 */

import { Requests, ResponseModel } from '@/utils/requests';
import { BlogApiPath } from '@/config/api_config';
import {
  BlogArticleItemType,
  BlogArticleListType,
} from '@/type/blog/blog_article_type';

/**
 * 获取博客文章
 * @constructor
 * @param page 第几页
 * @param searchFiled 搜索字段值
 */
export const BlogGetArticleListApi = async (page: number, searchFiled: any) => {
  searchFiled.page = page;
  const model = await Requests.defaults.httpNo<BlogArticleListType>(
    'post',
    BlogApiPath.blogGetArticleList,
    {
      postData: searchFiled,
    },
  );
  let articleList: Array<BlogArticleItemType> = [];
  let morePage = false;
  let success = false;
  if (model.success) {
    success = true;
    model.data!.data.lists.forEach((item) => {
      articleList.push(item);
    });
    // 用于判断是否还有下一页
    if (model.data!.data.last_page === 0) {
      morePage = false;
    } else {
      morePage = model.data!.data.current_page !== model.data!.data.last_page;
    }
  }
  return { success, morePage, articleList };
};

/**
 * 获取文章详情
 * @param id
 * @constructor
 */
export const BlogGetArticleApi = async (id: number) => {
  return Requests.defaults.httpNo<BlogArticleItemType>(
    'get',
    BlogApiPath.blogGetArticle + `${id}`,
  );
};
